package com.spider.service;

import com.spider.entity.CmsCertEntity;
import com.spider.mapper.OssMapper;
import com.spider.mapper.OssWebMapper;
import com.spider.oss.FileInfo;
import com.spider.oss.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Slf4j
public class WebProductServiceImpl {

    @Autowired
    OssMapper ossMapper;
    @Autowired
    OssWebMapper ossWebMapper;

    public String synCertListOnce(){
        List<FileInfo> fileInfos = ossMapper.selectAll();
        List<CmsCertEntity> cmsList =new ArrayList<>();
        for(FileInfo fileInfo:fileInfos){
            CmsCertEntity cmsCertEntity = OssUtil.convertCmsCert(fileInfo.getFilename());
            cmsCertEntity.setName(fileInfo.getFilename());
            cmsCertEntity.setUrl(fileInfo.getUrl());
            cmsCertEntity.setDelFlag(0);
            cmsCertEntity.setType(0);
            cmsCertEntity.setCreateTime(new Date());
            cmsList.add(cmsCertEntity);
        }
        // 每 2000 条数据进行一次批量插入
        int batchSize = 300;
        int num=1;
        for (int i = 0; i < cmsList.size(); i += batchSize) {
            log.info("第{}次批量插入",num);
            int endIndex = Math.min(i + batchSize, cmsList.size());
            List<CmsCertEntity> batchFiles = cmsList.subList(i, endIndex);
            ossWebMapper.saveFiles(batchFiles);
        }
        return "success";
    }
}
